//
//  SpeechVC.m
//  AVFoundationDemo
//
//  Created by zl on 2019/12/6.
//  Copyright © 2019 LG. All rights reserved.
//

#import "SpeechVC.h"
#import <AVFoundation/AVFoundation.h>

@interface SpeechVC () <AVSpeechSynthesizerDelegate>

@property (strong, nonatomic) AVSpeechSynthesizer *synthesizer;

@end

@implementation SpeechVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpView];
}
- (void)setUpView {
    UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake((self.view.frame.size.width-200)/2,150,200,40);
    [button setTitle:@"讲" forState:UIControlStateNormal];
    [button setTitle:@"停" forState:UIControlStateSelected];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.backgroundColor=[UIColor orangeColor];
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.showsTouchWhenHighlighted=YES;
    [button addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
- (void)start:(UIButton *)sender {
    if(sender.selected == NO) {
        if([_synthesizer isPaused]) {
            [_synthesizer continueSpeaking];
            sender.selected = !sender.selected;
        } else {
            _synthesizer = [[AVSpeechSynthesizer alloc] init];
            _synthesizer.delegate = self;
            AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"锦瑟无端五十弦，一弦一柱思华年。庄生晓梦迷蝴蝶，望帝春心托杜鹃。沧海月明珠有泪，蓝田日暖玉生烟。此情可待成追忆，只是当时已惘然。Very! I have always felt si misunderstood"];
            utterance.rate = 0.5;
            AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
            utterance.voice = voice;
            [_synthesizer speakUtterance:utterance];
            sender.selected = !sender.selected;
        }
    } else {
        [_synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryWord];
        [_synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryWord];
        sender.selected = !sender.selected;
    }
}
#pragma mark -
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"--------开始播放");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"-------取消播放");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"----------结束播放");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"----------暂停");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"----------继续播放");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance {
    
}

@end
